package fpeas.function;

public class FunctionUtility
{
	public static <T,R> Function<T,R> constant(final R value)
	{
		return new Function<T,R>()
		{
			public R run(final T t)
			{
				return value;
			}
		};
	}

	public static Function<Integer, Boolean> equal(final int i)
	{
		return new Function<Integer,Boolean>()
		{
			public Boolean run(final Integer integer)
			{
				return integer==i;
			}
		};
	}

	public static <T> Function<T,T> identity()
	{
		return new Function<T,T>()
		{
			public T run(final T t)
			{
				return t;
			}
		};
	}

	public static <T> Function<T,Integer> minus(final Function<T,Integer> first, final Function<T,Integer> second)
	{
		return new Function<T,Integer>()
		{
			public Integer run(final T t)
			{
				return first.run(t)-second.run(t);
			}
		};
	}

	public static <T> Function<T,Integer> add(final Function<T,Integer> first,final Function<T,Integer> second)
	{
		return new Function<T,Integer>()
		{
			public Integer run(final T t)
			{
				return first.run(t)+second.run(t);
			}
		};
	}

	public static <T,R,X> Function<T,X> compose(final Function<T,R> first, final Function<R,X> second)
	{
		return new Function<T,X>()
		{
			public X run(final T t)
			{
				return second.run(first.run(t));
			}
		};
	}
}